/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.QuizDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Quiz;
import models.User;

/**
 * this sevlet prepares the manage quizzes page and sets a list of courses.
 * The quizzes itself will be set in the jsp
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class QuizController extends HttpServlet {
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //connection using Dao
            HibernateUtil hu = new HibernateUtil();
            CourseDao cd = new CourseDao(hu.openSession());
            
            //Checks the role. if its an admin ALL courses will be shown
            List<Course> courseList = null;
            if(request.getSession().getAttribute("userrole").equals("Administrator")){
                courseList = cd.findAll();
            } else{
                User user = (User) request.getSession().getAttribute("user");
                courseList = cd.findAllByTeacher(user.getMember_id());
            }
            request.setAttribute("courses", courseList);
            hu.close();

            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("Quiz", "Manage quizzes")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);
            
            RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/manage_mycourse_quizzes.jsp");
            dispatcher.forward(request, response);
        }
}
    




